package com.myself.community.controller;

import com.myself.community.entity.Comment;
import com.myself.community.entity.DiscussPost;
import com.myself.community.entity.Event;
import com.myself.community.event.EventProducer;
import com.myself.community.service.CommentService;
import com.myself.community.service.DiscussPostService;
import com.myself.community.utils.Code;
import com.myself.community.utils.HostHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;

@Controller
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private EventProducer eventProducer;

    @Autowired
    private DiscussPostService discussPostService;

    /**
     * 评论帖子的请求
     * @param discussPostId
     * @param comment
     * @return
     */
    @PostMapping("/add/{discussPostId}")
    public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment) {
        if(StringUtils.isBlank(comment.getContent()))
            return "redirect:/discuss/detail/" + discussPostId;
        //补充评论信息其余信息
        comment.setUserId(hostHolder.getUser().getId());
        comment.setStatus(0);
        comment.setCreateTime(new Date());
        commentService.addComment(comment);

        //评论后添加信息
        //触发评论事件
        Event event = new Event()
                .setTopic(Code.TOPIC_COMMENT)
                .setUserId(hostHolder.getUser().getId())
                .setEntityType(comment.getEntityType())
                .setEntityId(comment.getEntityId())
                .setData("postId",discussPostId);//帖子id
        //若评论的是帖子
        if(comment.getEntityType()==Code.ENTITY_TYPE_POST){
            //查询出帖子的信息
            DiscussPost discussPost = discussPostService.findDiscussPostById(comment.getEntityId());
            //封装事件对象中的：实体作者的id
            event.setEntityUserId(discussPost.getUserId());
        }
        //若评论的是一个评论
        else if (comment.getEntityType()==Code.ENTITY_TYPE_COMMENT) {
            //查询出评论的信息
            Comment comment2 = commentService.findCommentById(comment.getEntityId());
            //封装事件对象中的：实体作者的id
            event.setEntityUserId(comment2.getUserId());
        }

        //生产者处理事件:发送消息到消息队列
        eventProducer.sendEvent(event);

        // 回复帖子相当于更改了帖子信息，需要触发事件  以便更新到es
        if(comment.getEntityType() == Code.ENTITY_TYPE_POST){
            event = new Event()
                    .setTopic(Code.TOPIC_PUBLISH)
                    .setUserId(comment.getUserId())
                    .setEntityType(Code.ENTITY_TYPE_POST)
                    .setEntityId(discussPostId);
            eventProducer.sendEvent(event);

            /*//计算帖子分数
            String redisKey = RedisUtil.getPostScoreKey();
            redisTemplate.opsForSet().add(redisKey,discussPostId);*/
        }

        return "redirect:/discuss/detail/" + discussPostId;
    }

}
